﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TaoNiuOA.Models;

namespace TaoNiuOA.Web.Controllers
{
    public class WorkTimeController : Controller
    {
        private DbEntity db = new DbEntity();

        public ViewResult Index()
        {
            var list = db.WorkTimes.ToList();

            //如果没数据则初始化
            if (list.Count == 0)
                Initialiton();

            return View(list);
        }

        //一周工作时间
        public void Initialiton()
        {
            var day = "星期一,星期二,星期三,星期四,星期五,星期六,星期天";
            var days = day.Split(',');
            var onWork = "09:00";
            var offWork = "18:00";
            for (var i = 0; i < days.Length; i++)
            {
                var wt = new TaoNiuOA.Models.WorkTime();
                wt.DayOfWeek = days[i];
                wt.OnWork = onWork;
                wt.OffWork = offWork;
                if (wt.DayOfWeek == "星期六")
                    wt.OffWork = "13:00";
                if (wt.DayOfWeek == "星期天")
                    wt.OnWork = wt.OffWork = "00:00";
                db.WorkTimes.Add(wt);
                db.SaveChanges();
            }
        }

        public ViewResult Details(int id)
        {
            WorkTime worktime = db.WorkTimes.Find(id);
            return View(worktime);
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(WorkTime worktime)
        {
            if (ModelState.IsValid)
            {
                db.WorkTimes.Add(worktime);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(worktime);
        }

        public ActionResult Edit(int id)
        {
            WorkTime worktime = db.WorkTimes.Find(id);
            return View(worktime);
        }


        [HttpPost]
        public ActionResult Edit(WorkTime worktime)
        {
            if (ModelState.IsValid)
            {
                db.Entry(worktime).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(worktime);
        }

        public ActionResult Delete(int id)
        {
            WorkTime worktime = db.WorkTimes.Find(id);
            return View(worktime);
        }


        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            WorkTime worktime = db.WorkTimes.Find(id);
            db.WorkTimes.Remove(worktime);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}